
package com.feib.ebilling.job.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.ebilling.job.dao.BillFeedbackDao;
import com.feib.ebilling.job.util.StringUtils;


/**
 * @title (#)BillFeedbackFailService.java<br>
 * @description <br>
 * @author Yaude Huang<br>
 * @version 1.0.0 2011/08/23
 * @copyright Far Eastern International Bank Copyright (c) 2011<br>
 * @2011/08/23 create by Yaude Huang<br>
 */
@Service("billFeedbackFailService")
public class BillFeedbackFailService extends BaseService  {
    
    @Resource(name="billFeedbackDao")
    private BillFeedbackDao billFeedbackDao;     
   
    /**
     * @return
     */
    public List<Map<String, Object>> loadGroupPK(String GroupNo) {
        return this.billFeedbackDao.queryGroupPKByGroupNo(GroupNo);
    }
    
    /**
     * @return
     */
    public List<Map<String, Object>> loadGroups() {
        return this.billFeedbackDao.queryALLGroup();
    }
    
    @SuppressWarnings("finally")
    /* 學校繳費銷帳失敗檔規格 */
	public String writeContentSchool(String groupNo, String groupPK, String processDate)
    {
    	StringBuffer sbf = new StringBuffer();    	
    	try{	    	    
	    	/* 首筆 */
    		sbf.append("1");
	    	sbf.append(groupNo);
	    	sbf.append(processDate);
	    	sbf.append(StringUtils.PadString("L", " ", 99,""));
	    	sbf.append("\r\n");
	    	/* 明細 */
	    	List<Map<String, Object>> payments = this.billFeedbackDao.queryWriteOffFailByGroupNo(groupPK, processDate);
	    	long total = 0;
	    	long amount = 0;        	
	    	for(Map<String, Object> payment : payments)
	        {
	    		sbf.append("2");
	    		/* 遠東商銀通路 */
	    		sbf.append(StringUtils.PadString("L", " ", 14, (String)payment.get("MRKEY")));
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("ADAY")));
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("ATIME")));
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("TBSDY")));
	    		sbf.append(StringUtils.PadString("L", " ", 1, (String)payment.get("APTYPE")));
	    		sbf.append(StringUtils.PadString("L", " ", 2, (String)payment.get("TXNO")));
	    		sbf.append(StringUtils.PadString("L", " ", 3, (String)payment.get("DSCPT")));
	    		BigDecimal bd = (BigDecimal) payment.get("TXAMT");
	    		/* 9(11)V99 */
	    		sbf.append(StringUtils.PadString("R", "0", 11, bd.toString()) + "00");
	    		sbf.append(StringUtils.PadString("L", " ", 16, (String)payment.get("USERNO")));
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("ATMID")));
	    		sbf.append(StringUtils.PadString("L", " ", 3, (String)payment.get("KINBR")));
	    		sbf.append(StringUtils.PadString("L", " ", 2, (String)payment.get("TRMSEQ")));
	    		sbf.append(StringUtils.PadString("L", " ", 5, (String)payment.get("TXTNO")));
	    		sbf.append(StringUtils.PadString("L", " ", 3, (String)payment.get("TFRBK")));
	    		sbf.append(StringUtils.PadString("L", " ", 16, (String)payment.get("TFRACT")));
	    		sbf.append(StringUtils.PadString("L", " ", 3, (String)payment.get("STANBK")));
	    		sbf.append(StringUtils.PadString("L", " ", 7, (String)payment.get("STANSEQ")));
	    		sbf.append(StringUtils.PadString("L", " ", 1, (String)payment.get("EC")));
	    		sbf.append(StringUtils.PadString("L", " ", 1, (String)payment.get("TRMTYP")));
	    		/* 超商通路資料 */
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("CHANNEL")));
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("STORE")));
	    		sbf.append(StringUtils.PadString("L", " ", 9, (String)payment.get("BARCODE1")));
	    		sbf.append(StringUtils.PadString("L", " ", 20, (String)payment.get("BARCODE2")));	
	    		sbf.append(StringUtils.PadString("L", " ", 15, (String)payment.get("BARCODE3")));
	    		sbf.append(StringUtils.PadStringChinese("L", " ", 100, (String)payment.get("ERR_MSG")));	
	    		sbf.append("E");
	    		total++;	    		
	    		amount = amount + Long.parseLong(bd.toString());
	    		sbf.append("\r\n");
	        }
	    	NumberFormat formatter = new DecimalFormat("#,###,###");

	    	logger.info("銷帳失敗筆數：{}" , formatter.format(total));
	    	logger.info("銷帳失敗金額：{}" , formatter.format(amount));
	    	/* 結尾 */
	    	sbf.append("3");
	    	sbf.append(groupNo);
	    	sbf.append(processDate);
	    	sbf.append(StringUtils.PadString("R", "0", 10, String.valueOf(total)));
	    	/* 9(14)V99 */
	    	sbf.append(StringUtils.PadString("R", "0", 14, String.valueOf(amount)) + "00");	    	
	    	sbf.append(StringUtils.PadString("L", " ", 73,""));
	    	sbf.append("\r\n");    	
    	 }catch (Exception e) {
            logger.error("[錯誤]" + e.getMessage(), e);
         }
         finally{
            return sbf.toString();            
         }
    }
	
}
